How to use Wait

您所在的位置:网站首页 powershell wait-process How to use Wait

How to use Wait

#How to use Wait| 来源: 网络整理| 查看: 265

How to use Wait-Process in Powershell? PowerShellMicrosoft TechnologiesSoftware & Coding

Wait-Process cmdlet in PowerShell is used to wait for the process to stop before the execution moves on to the next step.

Example

We have a snipping tool application running and we need to wait for the process to stop first and then move on to the next step.

PS C:\> Get-Process SnippingTool | Select Name,Id,CPU Name          Id    CPU ----          --    --- SnippingTool  7440   2.0625

To wait for the process to stop first we will use the Wait-Process command. You can provide ProcessName or ID.

Write-Output "Waiting for the Process to Stop" Wait-Process -Name 'SnippingTool' Write-Output 'Snipping tool Process is stopped'OutputPS C:\> C:\Temp\TestPS1.ps1 Waiting for the Process to Stop

Once the process is stopped,

PS C:\> C:\Temp\TestPS1.ps1 Waiting for the Process to Stop Snipping tool Process is stopped

If the desired process can’t stop, the execution waits forever. You can also provide the timeout parameter in seconds so if the process can’t stop in the mentioned time, it will throw an error.

ExampleWrite-Output "Waiting for the Process to Stop" Wait-Process -Name 'SnippingTool' -Timeout 5 Write-Output 'Snipping tool Process is stopped'OutputPS C:\> C:\Temp\TestPS1.ps1 Waiting for the Process to Stop CloseError: (System.Diagnostics.…cess (SnippingTool):Process) [Wait-Process], TimeoutException Snipping tool Process is stopped

In such instance we can use the change the ErrorAction setting to Stop.

ExampleWrite-Output "Waiting for the Process to Stop" Wait-Process -Name 'SnippingTool' -Timeout 5 -ErrorAction Stop Write-Output 'Snipping tool Process is stopped'OutputPS C:\> C:\Temp\TestPS1.ps1 Waiting for the Process to Stop CloseError: (System.Diagnostics.…cess (SnippingTool):Process) [Wait-Process], TimeoutException Chirag Nagrekar Chirag Nagrekar

Updated on: 04-Jan-2021

918 Views

Related ArticlesHow to use 鈥揥ait parameter in Restart-Computer cmdlet in PowerShell? PowerShell: Wait for the First command to finish What is the use of Get-Process command in PowerShell? How to use stopwatch in PowerShell? How to confirm before stopping the process in PowerShell? How to use Measure-Object in PowerShell? How to use Hashtable splatting in PowerShell? How to use Array Splatting in PowerShell? How to use function Alias in PowerShell? How to use a transcript in PowerShell? How to use Compare-Object in PowerShell? What is the use of Passthru parameter in Stop-Process in PowerShell? How to use PowerShell as a Calculator? Or How to use PowerShell for arithmetic operations? How to get process output in GridView format in PowerShell ? How to stop all instances of the process in PowerShell? Kickstart Your Career

Get certified by completing the course

Get Started Advertisements


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3